package com.ixiaowo.springcouldprovideuser.controller;

import com.google.common.collect.Lists;
import com.ixiaowo.springcouldprovideuser.entity.User;
import com.ixiaowo.springcouldprovideuser.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController //组合注解
public class UserController {
    @Resource
    private UserRepository userRepository;
    @GetMapping("/simple/{id}")
     public User findById(@PathVariable Long id){
        System.out.println( userRepository.findOne(id));
        return userRepository.getOne(id);
     }

    @Resource(name = "eurekaClient")
    private EurekaClient discoveryClient;
    @GetMapping(value = "/eureka-instance")
        public String serviceUrl() {
        //MICROSERVICE-PROVIDER-USER 默认Eureka application的名称
            InstanceInfo instance = discoveryClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
            return instance.getHomePageUrl();
        }

    /**
     * 测试 feign post请求方式
     * @param user
     * @return
     */
    @PostMapping("/user")
    public User postUser(@RequestBody User user){
        return user;
    }
    /**
     * 获取用户列表
     */
    @GetMapping("user-all")
    public List<User> getUserAll(){
        List<User> list =   Lists.newArrayList();
        User user = new User(1L,"张三");
        User user2 = new User(2L,"李四");
        User user3 = new User(3L,"王五");
        list.add(user);
        list.add(user2);
        list.add(user3);
        return list;
    }
}
